/*****************************************************************************
 * (C) Copyright 2004 Atech-Automotive
 * FILE NAME:    diagnostics.h
 * DESCRIPTION:  Header file for diagnostics.c
 * DATE BEGUN:   Mar 2009
 * BY:           Ralf Rosenbaum (M11), Roger Qiao 
 * PRODUCT NAME: M14 BCM
 * APPLICATION:  
 * TARGET H/W:   
 * DOC REF:
 *****************************************************************************
 */

#ifndef diag_h
#define diag_h

/*****************************************************************************
 *  I D E N T                                                                *
 *****************************************************************************/
#ifdef USE_IDENT
    #ident "$Id: diagnostics.h,v 1.9 2008/04/22 03:24:03 AndrewBeal Exp $"
#endif

/* #include */
#include "system.h"
#include "hwa_inputs.h"
#include "diag_cfg.h"


/* #define */
#define DTCR_OFFS_INVAL         0xFF

/* LIN rain sensor errors */
#define RS_COMM_FAULT           BIT0
#define RS_LIN_FAULT            BIT1
#define RS_FAULT                BIT4
#define RS_ERR_MASK             (RS_COMM_FAULT | RS_FAULT)

/* LIN PSD errors , will change per new spec*/
#define PSD_COMM_FAULT           BIT0
#define PSD_LIN_FAULT            BIT1
#define PSD_FAULT                BIT4
#define PSD_ERR_MASK             (PSD_COMM_FAULT | PSD_FAULT)

/* LIN reverse radar errors, will change per new spec */
#define REV_RADAR_COMM_FAULT           BIT0
#define REV_RADAR_LIN_FAULT            BIT1
#define REV_RADAR_FAULT                BIT4
#define REV_RADAR_ERR_MASK             (REV_RADAR_COMM_FAULT | REV_RADAR_FAULT)

/* LIN siren errors, not used in H13, but keep for legacy */
#define SIREN_SPK_FAIL          BIT0
#define SIREN_BAT_FAIL          BIT1
#define SIREN_LIN_FAULT         BIT2
#define SIREN_ERR_MASK          (SIREN_SPK_FAIL | SIREN_BAT_FAIL)

#define WIPER_NO_PARK_SIG       BIT6

/* ioctl commands */
#define CLR_DTC_REQ             0
#define DIAG_FFOG               1
//	#define DIAG_RFOG               2	//rz
//	#define DIAG_NO_FOG             3	//rz
#define REAR_WIP_WASH_DTC_ENABLE  4		//rz
#define REAR_WIP_WASH_DTC_DISABLE 5		//rz
#define IOCTL_DTC_STOP			6
#define IOCTL_DTC_START		7


/* bytes to be transmitted for each DTC */
#define STAT_NOT_SUPP           0x0F    /* statusOfDTC not supported */
#define STAT_DTC_ACT            0x00    /*rz only request dtc causing problems */
#define STAT_DTC_ALL            0x01    /*rz request all supported DTC */
#define BYTES_PER_DTC           3       /* 2 bytes DTC-value + 1 byte status */

/* CMD definition */
#define GET_NO_OF_ACT_DTCS      0
#define GET_DTCS_AND_FREEZE     1
#define FILL_ARRAY              2
#define CHK_CUR_FAIL_STS		3		/*rz check if there is any fails   */

/*rz DTC status definition */
//rz fault sympton
#define DTCFS_ZERO              0x00    /*rz symptom not avaliable         */
#define DTCFS_ABOVE             0x01    /*rz above maximum threshold       */
#define DTCFS_BELOW             0x02    /*rz below minimum threshold       */
#define DTCFS_NOSIGNAL          0x04    /*rz no signal                     */
#define DTCFS_BADSIGNAL         0x08    /*rz invalid signal                */
#define DTCFS_SHORT_BAT         0x03    /*rz short Battery                 */
#define DTCFS_SHORT_GND         0x05    /*rz short GND                     */
#define DTCFS_SHORT             0x07    /*rz short circuit                 */
#define DTCFS_OPEN              0x09    /*rz Open circuit                  */
#define DTCFS_INTNL_ERR         0x0A    /*rz Internal error                */
#define DTCFS_NETWK_ERR         0x0B    /*rz Network error                 */
//rz storage status
#define DTCSS_NOT_PRESENT       0x00    /*rz no DTC now or before          */
#define DTCSS_PRESENT_LAST_TIME 0x01    /*rz present before but not now    */
#define DTCSS_INTERMITTENT      0x02    /*rz not present but potential err */
#define DTCSS_PRESENT           0x03    /*rz currently present             */
//rz masks for status flags
#define DTC_TST_NOT_COMPLETE    BIT4    /*rz trouble test competed or not  */
#define DTC_WARNING_LAMP_EN     BIT7    /*rz warning lamp on or not        */


#define DTC_VBAT		0x12	//	Circuit short to battery
#define DTC_OPEN		0x13	//	Circuit open
#define DTC_LO_V		0x16	//	Circuit voltage below threshold
#define DTC_HI_V		0x17	//	Circuit voltage above threshold
#define DTC_LO_I		0x18	//	Circuit current below threshold
#define DTC_HI_I		0x19	//	Circuit current above threshold
#define DTC_NOTCONFIG	0x55	//	Not Configured
#define DTC_STUCK		0x71	//	Actuator stuck
#define DTC_MIS_MSG	0x87	//	Missing Message
#define DTC_BUS_OFF	0x88	//	Bus Off


#define CMD_RD_ACT_DTC	9
#define CMD_RD_HIS_DTC		8

#define FAIL_BIT			BIT0
#define FAIL_OPER_CYC_BIT	BIT1	//fail in this operation cycle
#define FAIL_CONFIR_BIT		BIT3
#define DTC_CONFIR_MASK	0x09 //(FAIL_BIT|FAIL_CONFIR_BIT)

#define HSD_NO_ERR	0
#define HSD_HI_I_ERR	1
#define HSD_LO_I_ERR	2

#define RELAY_NO_ERR	0
#define RELAY_OPEN_LOAD_ERR	1
#define RELAY_STUCK_ON_ERR	2
#define RELAY_GND_ERR		4

#define LSD_NO_ERR		0
#define LSD_BAT_ERR	1
#define LSD_OPEN_LOAD_ERR	2
/****dy*****/
#define DIAG_VBAT_NO_ERR 	0
#define DIAG_VBAT_H_CURR_ERR 	1
#define DIAG_VBAT_H_CONFIRM_ERR 	8
#define DIAG_VBAT_L_CURR_ERR 	1
#define DIAG_VBAT_L_CONFIRM_ERR 	8




typedef struct DTCBufStruct
{
	Byte dtcBuf[MAX_DTCS_STORED];
	Byte head;
}DTCBuf;

typedef enum _DTCE2State {
    DTC_E2_IDLE,
    DTC_E2_WAIT_RD,
    DTC_E2_WAIT_WR
} DTCE2State;

typedef struct _DTCStatus
{
	Byte status;
	Byte enable;
	Byte diag_In;
}DTCStatus;


/* Variables */
#ifdef diag_c

	DTCStatus dtcStatus[NUM_OF_DTCS];
	DTCBuf histDTCBuf;
	Bit F_histDTCUpdated; 

#else
    extern DTCStatus dtcStatus[];
    extern	DTCBuf histDTCBuf;
    extern Bit F_histDTCUpdated; 
#endif

extern Byte F_IOCtrl;	
/* typedef */

typedef struct _HSDDiagInfo
{
    Byte delayTime;
    Byte Counter;
    Byte delayCounter;
    Byte highTh;
    Byte lowTh;
    Byte a2dIdx;      
    Byte errStatus;
    Byte a2dBuffer[A2D_AVG_SIZE];
    Word F_outp:1;
    Word F_prevOutp:1;
} HSDDiagInfo;



typedef struct _LSDDiagInfo
{
    Byte delayTime;
    Byte delayCounter;
    Byte errStatus;
    Word F_outp:1;
    Word F_prevOutp:1;
} LSDDiagInfo;

typedef struct _VBATDiagInfo
{
    Byte ConfirmTimesH;
	Byte ConfirmTimesL;
	Byte ConfirmTimesNOR; 
    Byte ConfirmCounterH;
	Byte ConfirmCounterL;
	Byte ConfirmCounterNOR;
    Byte a2dIdx;      
    Byte errStatusH;
	Byte errStatusL;
    Byte a2dBuffer[A2D_AVG_SIZE];
} VBATDiagInfo;

typedef struct _TemperDiagInfo
{
	Byte delayTimeCounter;
    Byte ConfirmTimesErr;
	Byte ConfirmTimesNOR; 
    Byte ConfirmCounterErr;
	Byte ConfirmCounterNOR;
    Byte a2dIdx;      
    Byte errStatus;
    Byte a2dBuffer[A2D_AVG_SIZE];
} TemperDiagInfo;

typedef struct _CommunicationDiagInfo
{
	Byte delayTime;
    Byte ConfirmTimesErr;
	Byte ConfirmTimesNOR; 
    Byte ConfirmCounterErr;
	Byte ConfirmCounterNOR;
    Byte errStatus;
} CommunicationDiagInfo;

/* Variables */

/* Function prototypes */
void diagHandler(void);

void iniDiagnostics(void);
void updateHistDTCBuf(Byte dtcSeq);
Byte getActiveDTCs(Byte cmd, Byte *pDstBuf);
void ioctlDiagnostics(Byte cmd);
void diagSetWiperErrStatus(Byte err);
void diagSetRainSnsErrStatus(Byte);
void diagSetLinSirenErrStatus(Byte);
Byte diagGetCCSwitchErr(void);
Byte rearFogControlledByFBCM(void);         //rz
Byte getNumOfSupportedDTCs(void);           //rz
void clearDTCs(void);
void readLParkDiag(void);
void readRParkDiag(void);
void readWasherDiag(void);
void readMirDefrDiag(void);
void updateDTC(Byte cmd, Byte id);
void checkHSD(HSDDiagInfo* diag,Byte diagData,Byte outputStatus,Byte* overCurrentStatus);
void check_VBAT(VBATDiagInfo* diag,Byte diagData);
void check_Temprature(TemperDiagInfo* diag ,Byte diagData);


void checkLSD(LSDDiagInfo *diag, Byte diagIn,Byte outputStatus, Byte* overCurrentStatus);
void checkLSD_PWM(LSDDiagInfo *diag, Byte diagIn,Byte outputStatus, Byte* overCurrentStatus);

void iniHSDDiag(HSDDiagInfo *diag,Byte lowTH,Byte highTH,Byte delayTime);
void iniLSDDiag(LSDDiagInfo *diag,Byte delayTime);
 void iniVBATDiag(VBATDiagInfo *diag);
 void iniTemperDiag(TemperDiagInfo *diag);


extern void configDiags(void);

#endif
